package com.byb.server.service.impl;

import com.byb.server.service.DieService;
import com.byb.server.domain.Die;
import com.byb.server.repository.DieRepository;
import com.byb.server.repository.search.DieSearchRepository;
import com.byb.server.service.dto.DieDTO;
import com.byb.server.service.mapper.DieMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Die.
 */
@Service
@Transactional
public class DieServiceImpl implements DieService{

    private final Logger log = LoggerFactory.getLogger(DieServiceImpl.class);

    private final DieRepository dieRepository;

    private final DieMapper dieMapper;

    private final DieSearchRepository dieSearchRepository;
    public DieServiceImpl(DieRepository dieRepository, DieMapper dieMapper, DieSearchRepository dieSearchRepository) {
        this.dieRepository = dieRepository;
        this.dieMapper = dieMapper;
        this.dieSearchRepository = dieSearchRepository;
    }

    /**
     * Save a die.
     *
     * @param dieDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public DieDTO save(DieDTO dieDTO) {
        log.debug("Request to save Die : {}", dieDTO);
        Die die = dieMapper.toEntity(dieDTO);
        die = dieRepository.save(die);
        DieDTO result = dieMapper.toDto(die);
       // dieSearchRepository.save(die);
        return result;
    }

    /**
     *  Get all the dice.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<DieDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Dice");
        return dieRepository.findAll(pageable)
            .map(dieMapper::toDto);
    }

    /**
     *  Get one die by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public DieDTO findOne(Long id) {
        log.debug("Request to get Die : {}", id);
        Die die = dieRepository.findOne(id);
        return dieMapper.toDto(die);
    }

    /**
     *  Delete the  die by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Die : {}", id);
        dieRepository.delete(id);
        //dieSearchRepository.delete(id);
    }

    /**
     * Search for the die corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<DieDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of Dice for query {}", query);
        Page<Die> result = dieSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(dieMapper::toDto);
    }
}
